﻿Imports DTO
Imports BUS


Public Class frmThanhLySach : Inherits DevComponents.DotNetBar.Office2007Form
    Private m_Nhanvien As CNhanVienDTO
    Public Property NhanVien() As CNhanVienDTO
        Get
            Return m_Nhanvien
        End Get
        Set(ByVal value As CNhanVienDTO)
            m_Nhanvien = value
        End Set
    End Property
    Private m_danhdau As Boolean = True

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        If (txtMaSach.Text = "") Then
            MessageBox.Show("Chưa nhập mã sách!", "Thong Bao")
            txtMaSach.Focus()
            m_danhdau = False
            Return
        End If

        Dim ms As Integer
        If (Not Integer.TryParse(txtMaSach.Text, ms)) Then
            MessageBox.Show("Mã sách phải là một số nguyên!", "Thong Bao")
            txtMaSach.Text = ""
            txtMaSach.Focus()
            m_danhdau = False
            Return
        End If

        Dim str As String = New CSachBUS().LayTenSach(ms)
        If (str = "") Then
            MessageBox.Show("Cuốn sách này không tồn tại, hoặc đã bị thanh lý!", "Thong Bao")
            txtMaSach.Focus()
            txtMaSach.Text = ""
            m_danhdau = False
            Return
        End If

        txtTenSach.Text = str
        m_danhdau = True
    End Sub

    Private Sub frmThanhLySach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' load danh sách lý do thanh lý vào combobox:
        Dim listlytl As List(Of CLyDoThanhLyDTO) = New CLyDoThanhLyBUS().LayDanhSachLyDoThanhLySach()
        For Each ldtl As CLyDoThanhLyDTO In listlytl
            cboLyDoThanhLy.Items.Add(ldtl)
        Next
        dtpNgayThanhLy.Value = DateTime.Now
        cboLyDoThanhLy.ValueMember = "Ma"
        cboLyDoThanhLy.DisplayMember = "Ten"
        cboLyDoThanhLy.SelectedIndex = 1
        txtMaNguoiThanhLy.Text = m_Nhanvien.Ma
        txtTenNguoiThanhLy.Text = m_Nhanvien.HoTen

    End Sub
    Private stt As Integer = 0
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click

        btnCheck.PerformClick()
        If Not m_danhdau Then
            Return
        End If
        Dim ldtldto As New CLyDoThanhLyDTO()
        ldtldto = cboLyDoThanhLy.SelectedItem

        For Each dr As DataGridViewRow In dgvDanhSachSachThanhLy.Rows
            If (dr.Cells("clMaSach").Value = txtMaSach.Text) Then
                Return
            End If
        Next

        dgvDanhSachSachThanhLy.Rows.Add(stt, txtMaSach.Text, txtTenSach.Text, ldtldto.Ten)

    End Sub
    Public Sub Reset()
        txtMaSach.Text = ""
        txtTenSach.Text = ""
    End Sub
    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Reset()
    End Sub


    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        dgvDanhSachSachThanhLy.Rows.Clear()
    End Sub

    Private Sub btnThanhLy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThanhLy.Click
        Dim stlbus As New CSachThanhLyBUS()
        Dim stldto As CSachThanhLyDTO
        For Each dgvr As DataGridViewRow In dgvDanhSachSachThanhLy.Rows
            If dgvr.IsNewRow Then
                Continue For
            End If
            stldto = New CSachThanhLyDTO()
            stldto.Ma = dgvr.Cells("clMaSach").Value
            stldto.Ten = dgvr.Cells("clTenSach").Value
            stldto.TenLyDo = dgvr.Cells("clLyDoThanhLy").Value
            stldto.MaNhanVien = Integer.Parse(txtMaNguoiThanhLy.Text)
            stlbus.CapNhatSachThanhLy(stldto)
        Next
        MessageBox.Show("Xóa thành công", "Thông báo")
        dgvDanhSachSachThanhLy.Rows.Clear()
    End Sub

End Class